//
//  AppDelegate.m
//  ZhouProject
//
//  Created by admin on 2014-12-15.
//  Copyright (c) 2014年 admin. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "YongHuXinXi.h"
#import "LoadingHelper.h"
#import "NetWorkingHelper.h"
#import "MyApplyViewController.h"
#import "MyInfoViewController.h"
#import "XiaLa.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];

        [self defaultSaveuseraccount:@""];
        [self defaultSavePassword:@""];
        
    }else{
        
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    if (true){ //[self defaultLoaduseraccount].length == 0) {
        LoginViewController * vc = [[LoginViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
        [nav.navigationBar setBarTintColor:RGBACOLOR(19, 93, 225, 1)];
        UIColor * color = [UIColor whiteColor];
        NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
        nav.navigationBar.titleTextAttributes = dict;
        self.window.rootViewController = nav;
    }
    else{
        [LoadingHelper showLoadingWithView:self.window];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            YongHuXinXi * model = [NetWorkingHelper DengLuWithuserName:[self defaultLoaduseraccount] userPwd:[self defaultLoadPassword]];
            NSMutableArray * arrxiala = [NetWorkingHelper XiaLa];
            dispatch_async(dispatch_get_main_queue(), ^{
                [LoadingHelper hiddonLoadingWithView:self.window];
                if ([model.message isEqualToString:@"0"]) {

                    
                    NSMutableArray *arr = [[NSMutableArray alloc]init];
                    
                    MyApplyViewController *vc = [[MyApplyViewController alloc]init];
                    vc.strcategoryId = model.strcategoryId;
                    UINavigationController * nav = [[UINavigationController  alloc]initWithRootViewController:vc];
                    [nav.navigationBar setBarTintColor:RGBACOLOR(19, 93, 225, 1)];
                    UIColor * color = [UIColor whiteColor];
                    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
                    nav.navigationBar.titleTextAttributes = dict;
                    vc.tabBarController.hidesBottomBarWhenPushed = YES;
                    vc.title = @"我的应用";

          
                    
                    [arr addObject:nav];
                    
                    MyInfoViewController *  vc1 = [[MyInfoViewController alloc]init];
                    vc1.struserId = model.struserId;
                    vc1.strloginName = model.strloginName;
                    vc1.strpassword = model.strpassword;
                    vc1.strcategoryId = model.strcategoryId;
                    vc1.strshopArea = model.strshopArea;
                    vc1.strshopName = model.strshopName;
                    vc1.arr = arrxiala;
                    UINavigationController * nav1 = [[UINavigationController  alloc]initWithRootViewController:vc1
                                                     ];
                    [nav1.navigationBar setBarTintColor:RGBACOLOR(19, 93, 225, 1)];
                    UIColor * color1 = [UIColor whiteColor];
                    NSDictionary * dict1=[NSDictionary dictionaryWithObject:color1 forKey:NSForegroundColorAttributeName];
                    nav1.navigationBar.titleTextAttributes = dict1;
                    vc1.tabBarController.hidesBottomBarWhenPushed = YES;
                    vc1.title = @"我的资料";
               

                   
                    
                    [arr addObject:nav1];
                    
                    
                    
                    UITabBarController *bar = [[UITabBarController alloc] init];
                    bar.viewControllers = arr;
                    bar.selectedIndex = 0;
                    bar.delegate = self;
                    UITabBarItem * item = [bar.tabBar.items objectAtIndex:0];
                    [item setTitlePositionAdjustment:UIOffsetMake(0, -13)];
                    [item setTitleTextAttributes:  @{ NSFontAttributeName: [UIFont systemFontOfSize:20]} forState:UIControlStateNormal];
                    UITabBarItem * item1 = [bar.tabBar.items objectAtIndex:1];
                    [item1 setTitlePositionAdjustment:UIOffsetMake(0, -13)];
                    [item1 setTitleTextAttributes:  @{ NSFontAttributeName: [UIFont systemFontOfSize:20]} forState:UIControlStateNormal];
                    self.window.rootViewController = bar;
                    
                    
                    
                    
                }
                else
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"登录失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alert show];
                }
            });
        });
    }

    return YES;
}
-(NSString *)defaultLoaduseraccount
{
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"useraccount"];
    
}
-(void)defaultSaveuseraccount:(NSString*)a

{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"useraccount"];
    
    [defaults synchronize];
    
}
-(NSString *)defaultLoadPassword
{
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"Password"];
    
}
-(void)defaultSavePassword:(NSString*)a

{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"Password"];
    
    [defaults synchronize];
    
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
